﻿using DFGK.IService;
using DFGK.Repository;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System.Collections.Generic;

namespace DFGK_WMS.WebAPI.Controllers.PC.物料管理
{
    /// <summary>
    /// 物料管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class MaterialController : BaseController
    {
        private readonly IMaterialService materialService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="materialService"></param>
        public MaterialController(IWebHostEnvironment environment, IMaterialService materialService) : base(environment)
        {
            this.materialService = materialService;
        }

        /// <summary>
        /// 添加物料
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMaterial(Material m)
        {
            if (string.IsNullOrEmpty(m.Code))
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool IsOK = materialService.AddMaterial(m,out string message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！编码已存在！", "500"));
        }

        /// <summary>
        /// 物料列表显示
        /// </summary>
        /// <param name="mname"></param>
        /// <param name="materialcategoryid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMaterialList(string? mname, int materialcategoryid = 0, int pageIndex = 1, int pageSize = 10)
        {

            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = materialService.GetMaterialList(mname, materialcategoryid, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 物料删除
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelMaterial(long mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = materialService.DelMaterial(mid);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 物料修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdMaterial(Material m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(m.Code))
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            m.ModifyUserId = base.BaseGetNowUserId();
            m.ModifyUserName = base.BaseGetNowUserName();
            bool IsOK = materialService.UpdMaterial(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！编码已存在！", "500"));
        }

        /// <summary>
        /// 根据Id获取对象
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMaterialById(long mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var mobj = materialService.GetMaterialById(mid);
            if (mobj != null)
            {
                return Ok(BaseResult.Result(mobj, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="excel"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadExcel(IFormFile excel)
        {
            if (excel == null || excel.Length == 0)
            {
                return Ok(BaseResult.Result("400", "没有选择文件或文件为空"));
            }
            var postFile = Request.Form.Files[0];
            string extName = Path.GetExtension(postFile.FileName);
            if (!new string[] { ".xls", ".xlsx" }.Contains(extName))
            {
                return Ok(BaseResult.Result("400", "必须是Excel文件"));
            }
            var stream = new MemoryStream();
            excel.CopyTo(stream);
            var rows = MiniExcel.Query<Material>(stream);

            List<Material> materiallist = new List<Material>();
            materiallist = rows.ToList();
            bool result = false;
            foreach (var mat in materiallist)
            {
                result = materialService.AddMaterial(mat,out string message);
                if (!result)
                    return Ok(BaseResult.Result(false, "添加失败！" + message, "200"));
            }
            return Ok(BaseResult.Result(true, "添加成功！", "200"));
        }

        /// <summary>
        /// 趋势统计导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TrendDown()
        {
            var list = materialService.GetMaterialsDown();
            if (list != null)
                return TrendDown(list);
            else
                return Ok();
        }

    }
}
